function viewSummary1() {
	document.getElementById('butnL').style.display = '';
	document.getElementById('butnS').style.display = 'none';
	switchLongOrShortViewByIndex('externalExposure', '170%', 'short',
			'7,8,16,18,20,25,26,29,31,32,36,37,38,39,40,41,42,43,44,45,46,47,48,49,50,54');
}

function viewSummary2() {
	document.getElementById('butnL').style.display = '';
	document.getElementById('butnS').style.display = 'none';
	switchLongOrShortViewByIndex(
			'externalExposure',
			'150%',
			'short',
			'7,8,16,18,20,25,26,29,31,32,34,35,36,37,38,39,40,41,42,43,44,45,46,47,48,49,50,54');
}

function showDisabledForAmendedAgreement(tableId) {
	var table = document.getElementById(tableId);
	if (table == '' || table == null || table == undefined || table.length < 1) {
	} else {
		var trRows = table.rows;
		if (trRows == '' || trRows == null || trRows == undefined
				|| trRows.length < 1) {
		} else {
			for ( var i = 1; i < trRows.length; i++) {
				var agreementStatus = trRows[i].cells[56].children[0].value;
				if (agreementStatus == 69) {
					trRows[i].cells[6].disabled = true;
					trRows[i].cells[7].disabled = true;
					trRows[i].cells[8].disabled = true;
					trRows[i].cells[9].disabled = true;
					trRows[i].cells[10].disabled = true;
					trRows[i].cells[11].disabled = true;
					trRows[i].cells[13].children[0].disabled = true;
					trRows[i].cells[14].disabled = true;
					trRows[i].cells[15].children[0].disabled = true;
					trRows[i].cells[16].children[0].disabled = true;
					trRows[i].cells[17].disabled = true;
					trRows[i].cells[21].disabled = true;
					trRows[i].cells[22].disabled = true;
					trRows[i].cells[26].disabled = true;
					trRows[i].cells[27].disabled = true;
					trRows[i].cells[30].children[0].children[0].disabled = true;
					trRows[i].cells[31].children[0].disabled = true;
					trRows[i].cells[32].disabled = true;
					trRows[i].cells[33].disabled = true;
					trRows[i].cells[34].disabled = true;
					trRows[i].cells[35].disabled = true;
					trRows[i].cells[36].children[0].children[0].disabled = true;
					trRows[i].cells[37].disabled = true;
					trRows[i].cells[39].disabled = true;
					trRows[i].cells[40].disabled = true;
					trRows[i].cells[41].disabled = true;
					trRows[i].cells[42].disabled = true;
					trRows[i].cells[43].disabled = true;
					trRows[i].cells[44].disabled = true;
					trRows[i].cells[45].disabled = true;
					trRows[i].cells[46].disabled = true;
					trRows[i].cells[47].disabled = true;
					trRows[i].cells[48].disabled = true;
					trRows[i].cells[49].disabled = true;
					trRows[i].cells[50].disabled = true;
					trRows[i].cells[55].disabled = true;
				}
			}
		}
	}
}

String.prototype.endWith = function(str) {
	if (str == null || str == "" || this.length == 0
			|| str.length > this.length)
		return false;
	if (this.substring(this.length - str.length) == str)
		return true;
	else
		return false;
	return true;
}

String.prototype.startWith = function(str) {
	if (str == null || str == "" || this.length == 0
			|| str.length > this.length)
		return false;
	if (this.substr(0, str.length) == str)
		return true;
	else
		return false;
	return true;
}

function getFundEvents(img, workflowId) {
	var dataFromBrowser = img;
	var callbackProxy = function(dataFromServer) {
		callbackFunc(dataFromServer, dataFromBrowser);
	};
	var callMetaData = {
		callback : callbackProxy
	};
	ExternalExposureHelper.getFundEvents(workflowId, callMetaData);
}

function callbackFunc(dataFromServer, dataFromBrowser) {
	var fundEvents = dataFromServer;
	var img = dataFromBrowser;
	if (img.id.startWith('img_') && img.src.endWith('img/actions/add.gif')) {
		var table = document.getElementById('externalExposure');
		var parentRow = img.parentNode.parentNode;
		var umbrellaWorkflowId = parentRow.cells[0].childNodes[0].value;
		var rows = table.rows;
		for ( var i = 0; i < rows.length; i++) {
			var row = rows[i];
			if (row.id.startWith(umbrellaWorkflowId + '_fund_')) {
				row.style.display = ''
			}
		}
		img.src = 'img/actions/minus.gif';
	} else if (img.src.endWith('img/actions/add.gif') && fundEvents != null
			&& fundEvents.length != null && fundEvents.length > 0) {
		var parentRow = img.parentNode.parentNode;
		//update fundsNum
		parentRow.cells[61].children[0].value = fundEvents.length;
		
		var text = parentRow.innerHTML;
		var index =text.substr(text.indexOf("[")+1,text.indexOf("]")-text.indexOf("[")-1);
		var rowId = 'parentRow' + index;
		parentRow.id = rowId;
		var lastRow = null;
		for ( var i = 0; i < fundEvents.length; i++) {
			var newRow = dwr.util.cloneNode(rowId);
			var cells = newRow.cells;
			var newPattern = "externalExposures[" + (index)
					+ "].fundEvents[" + (fundEvents.length - i - 1) + "]";
			for ( var j = 0; j < cells.length; j++) {
				var cell = cells[j];
				var baseHTML = cell.innerHTML;
				var newHTML = baseHTML.replace(/externalExposures\[\d+\]/g,
						newPattern);
				cell.innerHTML = newHTML;
			}

			parentRow.swapNode(newRow);
			setFundEventValue(fundEvents[i], newPattern, newRow);
		}
		img.src = 'img/actions/minus.gif';
		img.id = 'img_' + index;
	} else if (img.src.endWith('img/actions/minus.gif')) {
		var table = document.getElementById('externalExposure');
		var parentRow = img.parentNode.parentNode;
		var umbrellaWorkflowId = parentRow.cells[0].childNodes[0].value;
		var rows = table.rows;
		for ( var i = 0; i < rows.length; i++) {
			var row = rows[i];
			if (row.id.startWith(umbrellaWorkflowId + '_fund_')) {
				row.style.display = 'none'
			}
		}
		img.src = 'img/actions/add.gif';
	}
}

function setFundEventValue(fundEvent, namePrefix, row) {
	if (fundEvent.marginCallStatus == 65 && fundEvent.issuedValue != 0) {
		row.style.backgroundColor='#FD823F';
	} else if (row.style.backgroundColor =='#FD823F' || row.style.backgroundColor =='#fd823f') {
		row.style.backgroundColor='';
	}
	
	var tabPage = dwr.util.getValue('tabPage');
	
	row.id = fundEvent.parentId + '_fund_' + fundEvent.workflowId;
	dwr.util.setValue(namePrefix + '.workflowId', fundEvent.workflowId);
	dwr.util.setValue(namePrefix + '.agreementId', fundEvent.agreementId);
	dwr.util.setValue(namePrefix + '.movementRequiredId', fundEvent.movementRequiredId);
	dwr.util.setValue(namePrefix + '.deadline', fundEvent.deadline);
	dwr.util.setValue(namePrefix + '.letterAction', fundEvent.letterAction);
	// link
	var linkCell = row.cells[5];
	var fundImg = getFirstChildNode(linkCell);
	fundImg.src = 'img/t.gif';
	fundImg.onclick = '';
	var haveAutoEmail = false;
	var haveBookAsset = false;
	var autoEmailOption = null;
	var bookAssetOption = null;
	if (linkCell.childNodes.length > 2) {
		var select = linkCell.childNodes[2];
		for ( var i = 0; i < select.options.length; i++) {
			var option = select.options[i];
			var theValue = option.value;
			var newPattern = "agreementId=" + fundEvent.agreementId;
			var newValue = theValue.replace(/agreementId=\d+/g, newPattern);
			newPattern = "workflowId=" + fundEvent.workflowId;
			newValue = newValue.replace(/workflowId=\d+/g, newPattern);
			newPattern = "principalId=" + fundEvent.principalId;
			newValue = newValue.replace(/principalId=\d+/g, newPattern);
			newPattern = "counterpartyId=" + fundEvent.counterpartyId;
			newValue = newValue.replace(/counterpartyId=\d+/g, newPattern);
			newPattern = "description=" + fundEvent.description;
			newValue = newValue.replace(/description=\D+/g, newPattern);
			newPattern = "_movement=" + fundEvent.movementRequired + "&";
			newValue = newValue.replace(/_movement=\D+&/g, newPattern);
			option.value = newValue;
			// fund event can not auto email
			if (option.innerText == 'Auto Email') {
				if (fundEvent.wrong == true
						|| !fundEvent.canAutoEmail
						|| fundEvent.agreementStatus != 68
						|| fundEvent.marginCallStatus != 65
						|| (fundEvent.eeInitTab != 1
								&& fundEvent.eeInitTab != 2 && fundEvent.eeInitTab != 3)) {
//					option.removeNode(true);
					autoEmailOption = option
				} else {
					haveAutoEmail = true;
				}
			} else if (option.innerText == 'Book Asset') {
				// when collateral at umbrella level remove book asset link
				if (fundEvent.wrong == true
						|| !fundEvent.canBookAsset
						|| fundEvent.agreementStatus != 68
						|| (fundEvent.historical == false && fundEvent.statementStatus == 500)) {
//					option.removeNode(true);
					bookAssetOption = option
				} else {
					haveBookAsset = true;
				}
			}
		}
		
		if (autoEmailOption != null) {
			autoEmailOption.removeNode(true);
		}
		
		if (bookAssetOption != null) {
			bookAssetOption.removeNode(true);
		}
		
		if (fundEvent.canAutoEmail
				&& haveAutoEmail == false
				&& fundEvent.wrong == false
				&& fundEvent.agreementStatus == 68
				&& fundEvent.marginCallStatus == 65
				&& (fundEvent.eeInitTab == 1 || fundEvent.eeInitTab == 2 || fundEvent.eeInitTab == 3)) {
			var oOption = document.createElement("OPTION");
			select.options.add(oOption);
			oOption.innerText = "Auto Email";
			oOption.value = "auto email";
		}

		if (fundEvent.canBookAsset
				&& haveBookAsset == false
				&& fundEvent.wrong == false
				&& fundEvent.agreementStatus == 68
				&& (fundEvent.historical == true || fundEvent.statementStatus != 500)) {
			var oOption = document.createElement("OPTION");
			select.options.add(oOption);
			oOption.innerText = "Book Asset";
			oOption.value = "/firmament/collateral/showAssetSummary.do?agreementId="
					+ fundEvent.agreementId
					+ "&movementRequired="
					+ fundEvent.movementRequired
					+ "&workflowId="
					+ fundEvent.workflowId;
		}
	}

	setInnerInput(row.cells[6], fundEvent._eventDate);
	setInnerText(row.cells[6], fundEvent._eventDate);
	
	setInnerInput(row.cells[7], fundEvent._agreementEventDate);
	setInnerText(row.cells[7], fundEvent._agreementEventDate);
	
	setInnerInput(row.cells[8], fundEvent.eventId);
	setInnerText(row.cells[8], fundEvent.eventId);
	
	row.cells[9].innerText = fundEvent.principalCode;
	row.cells[10].innerText = fundEvent.counterpartyName;
	row.cells[11].innerText = fundEvent.description;

	var stmtStatusCell = row.cells[12];
	if (fundEvent.statementStatus == 68) {
		stmtStatusCell.innerHTML = '<IMG src="img/tickg.gif" width=13 height=13>';
	} else if (fundEvent.statementStatus == 500) {
		stmtStatusCell.innerHTML = '<div>IP</div>';
	} else {
		stmtStatusCell.innerHTML = '<IMG src="img/crossg.gif" width=13 height=13>';
	}
	
	var workflowStatusCell = row.cells[13];
	var workflowStatusSelect = getFirstChildNode(workflowStatusCell);
	dwr.util.removeAllOptions(workflowStatusSelect);
	var approveOption = document.createElement("OPTION");
	workflowStatusSelect.options.add(approveOption);
	approveOption.innerText = "Approved";
	approveOption.value = 68;
	if (fundEvent.workflowStatus == 69) {
		var amendedOption = document.createElement("OPTION");
		workflowStatusSelect.options.add(amendedOption);
		amendedOption.innerText = "Amended";
		amendedOption.value = 69;
	} else if (fundEvent.workflowStatus == 70) {
		var amendedOption = document.createElement("OPTION");
		workflowStatusSelect.options.add(amendedOption);
		amendedOption.innerText = "Amended-Waived";
		amendedOption.value = 70;
	}
	dwr.util.setValue(workflowStatusSelect, fundEvent.workflowStatus);
	workflowStatusCell.children[1].value = fundEvent.workflowStatus;
	
	row.cells[14].innerText = fundEvent.calcAgent;
	
	var callStatusCell = row.cells[15];
	var callStatusSelect = getFirstChildNode(callStatusCell);
	if (fundEvent.callStatusesNoEditPrivilege != null) {
		dwr.util.removeAllOptions(callStatusSelect);
		dwr.util.addOptions(callStatusSelect, fundEvent.callStatusesNoEditPrivilege, false);
	}
	dwr.util.setValue(callStatusSelect, fundEvent.marginCallStatus);
	callStatusCell.children[1].value = fundEvent.marginCallStatus;
	callStatusCell.children[2].value = fundEvent.marginCallStatus;
	
	var userDefinedCallStatusCell = row.cells[16];
	var userDefinedCallStatusSelect = getFirstChildNode(userDefinedCallStatusCell);
	dwr.util.setValue(userDefinedCallStatusSelect, fundEvent.userDefinedCallStatus);
	
	var marginTypeCell = row.cells[17];
	marginTypeCell.innerText = '';
	if (fundEvent.nonNet == true) {
		marginTypeCell.innerText = 'Non-Netted';
	} else {
		if (fundEvent.eventSource == 0) {
			marginTypeCell.innerText = 'Net';
		} else if (fundEvent.eventSource == 0) {
			marginTypeCell.innerText = 'Auto-Net';
		}
	}
	
	var rbcCell = row.cells[18];
	if (fundEvent.released == true) {
		rbcCell.innerHTML = '<IMG src="img/tickg.gif" width=13 height=13>';
	} else {
		rbcCell.innerHTML = '';
	}
	
	var sdbCell = row.cells[19];
	if (fundEvent.sysDraftPresent == true) {
		sdbCell.innerHTML = '<IMG src="img/tickg.gif" width=13 height=13>';
	} else {
		sdbCell.innerHTML = '';
	}
	
	var clbCell = row.cells[20];
	if (fundEvent.clBreached == true) {
		clbCell.innerHTML = '<IMG src="img/tickg.gif" width=13 height=13>';
	} else {
		clbCell.innerHTML = '';
	}
	
	setInnerInput(row.cells[21], fundEvent.movementRequiredId);
	setInnerText(row.cells[21], fundEvent.movementRequired);
	
	setInnerInput(row.cells[22], fundEvent.marginCall);
	setInnerText(row.cells[22], fundEvent._marginCall);
	
	var ctpyAmtCell = row.cells[23];
	if (getFirstChildNode(ctpyAmtCell).tagName == 'INPUT') {
		dwr.util.setValue(namePrefix + '._marginDelvCtpy', fundEvent._marginDelvCtpy);
	} else {
		setInnerText(row.cells[23], fundEvent._marginDelvCtpy);
	}
	var ctpyAmtOld =  row.cells[23].children['_marginDelvCtpy_old'];
	ctpyAmtOld.value  =  fundEvent._marginDelvCtpy;
	
	if (ctpyAmtCell.children.length > 2) {
		ctpyAmtCell.children[2].removeNode(true);
	}
	
	if (ctpyAmtCell.children.length > 2) {
		ctpyAmtCell.children[2].removeNode(true);
	}
	
	var ctpyAmtUmbCcyCell = row.cells[24];
	if (getFirstChildNode(ctpyAmtUmbCcyCell).tagName == 'INPUT') {
		dwr.util.setValue(namePrefix + '._marginDelvCtpyUmbBaseCcy', fundEvent._marginDelvCtpyUmbBaseCcy);
	} else {
		setInnerText(ctpyAmtUmbCcyCell, fundEvent._marginDelvCtpyUmbBaseCcy);
	}

	var cevCell = row.cells[25];
	if (fundEvent.verified == true) {
		cevCell.innerHTML = '<IMG src="img/tickg.gif" width=13 height=13>';
	} else {
		cevCell.innerHTML = '';
	}
	
	setInnerInput(row.cells[26], fundEvent.stdAmount);
	setInnerText(row.cells[26], fundEvent._stdAmount);
	
	var sysCalcAmtCell = row.cells[27];
	sysCalcAmtCell.children[0].value = fundEvent._agreedAmount;
	sysCalcAmtCell.children[1].value = fundEvent._agreedAmount;
	sysCalcAmtCell.children[2].value = fundEvent._agreedAmount;
	var baseHTML = sysCalcAmtCell.children[2].outerHTML;
	var sysCalcAmtCellName = sysCalcAmtCell.children[2].name;
	var pattern = "_index" + fundEvent.agreementId + fundEvent.movementRequired;
	var newHTML = baseHTML.replace(sysCalcAmtCellName, pattern);
	sysCalcAmtCell.children[2].outerHTML = newHTML;
	
	var issuedValueCell = row.cells[28];
	if (fundEvent._issuedValue != null) {
		issuedValueCell.innerText = fundEvent._issuedValue;
		dwr.util.setValue(namePrefix + '._issuedValue', fundEvent._issuedValue);
		issuedValueCell.innerHTML = issuedValueCell.innerHTML + '<INPUT class=input value=' + fundEvent._issuedValue + ' type=radio name=_index' + fundEvent.agreementId + fundEvent.movementRequired + '>'
		if (issuedValueCell.children[1] != null) {
			baseHTML = issuedValueCell.children[1].outerHTML;
			newHTML = baseHTML.replace(/index\d+\D+/g, pattern);
			issuedValueCell.children[1].outerHTML = newHTML;
		}
	} else {
		issuedValueCell.innerText = '';
		setInnerInput(issuedValueCell, '');
		if (issuedValueCell.children[1] != null) {
			issuedValueCell.children[1].removeNode(true);
		}
	}

	setInnerInput(row.cells[29], fundEvent.issueDate);
	setInnerText(row.cells[29], fundEvent._issueDate);

	var instrumentIdCell = row.cells[30];
	var instrumentImg = instrumentIdCell.children[0].children[2];
	if (fundEvent.statementStatus != 500 && fundEvent.agreementStatus == 68
			&& fundEvent.canBookAsset) {
		instrumentImg.style.cssText = 'cursor:pointer';
	} else {
		instrumentImg.style.cssText = 'cursor:pointer;display:none';
	}
	
	instrumentIdCell.children[0].children[3].value = fundEvent.movementRequired;
	instrumentIdCell.children[1].value = fundEvent.agreementId;
	instrumentIdCell.children[2].value = fundEvent.workflowId;
	if (fundEvent.agreementStatus != 68 || fundEvent.statementStatus == 500) {
		if (instrumentIdCell.children.length > 3) {
			instrumentIdCell.children[0].children[2].removeNode(true);
		}
	}
	
	var userAgreedAmtCell = row.cells[31];
	if (userAgreedAmtCell.children[0].tagName == 'INPUT') {
		userAgreedAmtCell.children[0].value = fundEvent._userAgreedAmount;
		userAgreedAmtCell.children[0].readOnly = !fundEvent.overwrite;
	} else {
		setInnerText(userAgreedAmtCell, fundEvent._userAgreedAmount);
	}
	
	var overWriteCell = row.cells[32];
	overWriteCell.children[0].checked = fundEvent.overwrite;
	if (overWriteCell.children.length > 1) {
		overWriteCell.children[1].value = fundEvent.overwrite;
	}
	
	var agreementBaseCcyCell = row.cells[33];
	agreementBaseCcyCell.children[0].value = fundEvent.currencyId;
	agreementBaseCcyCell.children[1].innerText = fundEvent.currency;
	
	var disputeAgeCell = row.cells[34];
	setInnerText2(disputeAgeCell, '');
	if (disputeAgeCell.children.length > 0 && disputeAgeCell.children[0].tagName == 'INPUT') {
		if (fundEvent.historical == true) {
			setInnerText2(disputeAgeCell, fundEvent.disputeAge);
		} else {
			if (fundEvent.disputedValue < 0) {
				setInnerText2(disputeAgeCell, fundEvent.disputeAge);
			}
		}
	} else {
		if (fundEvent.disputedValue < 0) {
			setInnerText2(disputeAgeCell, fundEvent.disputeAge);
		}
	}
	
	row.cells[35].innerText = fundEvent._disputedValue;
	
	var commentsCell = row.cells[36];
	if (fundEvent.comments != null) {
		commentsCell.children[0].children[0].innerText = fundEvent.comments;
	}else{
		commentsCell.children[0].children[0].innerText = "";
	}
	if (fundEvent.agreementStatus != 68) {
		if (commentsCell.children.length > 1) {
			commentsCell.children[1].removeNode(true);
		}
	}
	
	row.cells[37].innerText = fundEvent._totalExposure;
	
	var ctpyExposureCell = row.cells[38];
	if (getFirstChildNode(ctpyAmtCell).tagName == 'INPUT') {
		dwr.util.setValue(namePrefix + '._cptyExposure', fundEvent._cptyExposure);
	} else {
		setInnerText(row.cells[38], fundEvent._cptyExposure);
	}
	
	row.cells[39].innerText = fundEvent._adjColValue;
	if (fundEvent.colManager != null) {
		row.cells[40].innerText = fundEvent.colManager;
	}
	row.cells[41].innerText = fundEvent.reciprocity;
	row.cells[42].innerText = fundEvent.principalName;
	row.cells[43].innerText = fundEvent.agreementType;
	row.cells[44].innerText = fundEvent._systemReq;
	row.cells[45].innerText = fundEvent.systemBaseCcy;
	row.cells[46].innerText = fundEvent.legalFrequencyPeriod;
	if (fundEvent.legalFrequencyDays != null) {
		row.cells[47].innerText = fundEvent.legalFrequencyDays;
	}
	row.cells[48].innerText = fundEvent.notifyBy;
	row.cells[49].innerText = fundEvent.resolveBy;
	row.cells[50].innerText = fundEvent.creditRating;
	dwr.util.setValue(namePrefix + '.eventSource', fundEvent.eventSource);
	dwr.util.setValue(namePrefix + '.eeInitTab', fundEvent.eeInitTab);
	dwr.util.setValue(namePrefix + '.historical', fundEvent.historical);
	row.cells[55].innerText = fundEvent.group;
	dwr.util.setValue(namePrefix + '.agreementStatus', fundEvent.agreementStatus);
	dwr.util.setValue(namePrefix + '.autoEmail', fundEvent.autoEmail);
	dwr.util.setValue(namePrefix + '.parentId', fundEvent.parentId);
	dwr.util.setValue(namePrefix + '.parentAgreementId', fundEvent.parentAgreementId);
	dwr.util.setValue(namePrefix + '.umbrellaType', fundEvent.umbrellaType);
	dwr.util.setValue(namePrefix + '.fundsNum', fundEvent.fundsNum);
	dwr.util.setValue(namePrefix + '.marketDataId', fundEvent.marketDataId);
	dwr.util.setValue(namePrefix + '.comments_old', fundEvent.comments);
}

function ctpyAmtChange(changedElement, umbCcyChange) {
	addChangedAgrId(changedElement);
	var movement = changedElement.parentNode.parentNode.cells[21].children[0].value;
	var flag;
	var inputValue = changedElement.value;
	
	if(movement == 51 || movement == 52) {
		if(parseFloat(inputValue) < 0) {
			flag = window.confirm("Entering this value implies a Full Dispute, Would you like to reverse the sign?");
		}
		
	}else if(movement == 53 || movement == 54) {
		if(parseFloat(inputValue) > 0) {
			flag = window.confirm("Entering this value implies a Full Dispute, Would you like to reverse the sign?");
		}
	}
	if(flag) changedElement.value = parseFloat(inputValue) < 0 ? inputValue.substring(1, inputValue.length) : "-" + inputValue;
	
	var umbrellaType = changedElement.parentNode.parentNode.cells[60].childNodes[0].value;
	if (umbrellaType == 2) {
		var workflowId = changedElement.parentNode.parentNode.cells[0].childNodes[0].value;
		var name = changedElement.parentNode.parentNode.cells[61].childNodes[0].name;
		var namePrefix = name.replace(/fundEvents\[\d+\].fundsNum/g, '');
		var fundsNum = dwr.util.getValue(namePrefix + 'fundsNum');
		var umbrellaCcy = dwr.util.getValue(namePrefix + 'currencyId');
		var marketDataId = dwr.util.getValue(namePrefix + 'marketDataId');
		var fundAmts = new Array();
		var ctpyAmtName = '';
		if (umbCcyChange == true) {
			ctpyAmtName = '_marginDelvCtpyUmbBaseCcy';
		} else {
			ctpyAmtName = '_marginDelvCtpy';
		}
		for ( var i = 0; i < fundsNum; i++) {
			var currency = dwr.util.getValue(namePrefix + 'fundEvents[' + i +'].currencyId');
			var ctpyAmt = dwr.util.getValue(namePrefix + 'fundEvents[' + i +'].' + ctpyAmtName);
			var actionId = dwr.util.getValue(namePrefix + 'fundEvents[' + i +'].movementRequiredId');
			var changed = false;
			if (workflowId == dwr.util.getValue(namePrefix + 'fundEvents[' + i +'].workflowId')) {
				changed = true;
			}
				
			var fundAmt = {amtValue:ctpyAmt, currency:currency, changed:changed, actionId:actionId};
			fundAmts.push(fundAmt);
		}

		var dataFromBrowser = {namePrefix:namePrefix, changedElement:changedElement, umbCcyChange:umbCcyChange};
		var callbackProxy = function(dataFromServer) {
			ctpyAmtChangeCallbackFunc(dataFromServer, dataFromBrowser);
		};
		var callMetaData = {
			callback : callbackProxy
		};
		
		ExternalExposureHelper.ctpyAmtChange(fundAmts, umbrellaCcy, marketDataId, umbCcyChange, callMetaData);
	}
} 

function ctpyAmtChangeCallbackFunc(dataFromServer, dataFromBrowser) {
	dwr.util.setValue(dataFromBrowser.namePrefix + '_marginDelvCtpy', dataFromServer.totalValue);
	dwr.util.setValue(dataFromBrowser.namePrefix + '_marginDelvCtpyUmbBaseCcy', dataFromServer.totalValue);
	if (dataFromBrowser.umbCcyChange == true) {
		dataFromBrowser.changedElement.parentNode.parentNode.cells[23].childNodes[0].value = dataFromServer.fundValue;
	} else {
		dataFromBrowser.changedElement.parentNode.parentNode.cells[24].childNodes[0].value = dataFromServer.fundValue;
	}
}

function addChangedAgrId(changedElement) {
	var agreementId = changedElement.parentNode.parentNode.cells[1].childNodes[0].value;
    document.getElementById("changedAgreement").value = document.getElementById("changedAgreement").value + ","+ agreementId;
} 

function addChangedAgrId2(changedElement) {
	var agreementId = changedElement.parentNode.parentNode.parentNode.cells[1].childNodes[0].value;
    document.getElementById("changedAgreement").value = document.getElementById("changedAgreement").value + ","+ agreementId;
}

function groupImplyCtpyAmt(img) {
	var umbrellaTR = img.parentNode.parentNode;
	var umbrellaActionId = umbrellaTR.cells[21].children[0].value;
	var expandImg = umbrellaTR.cells[5].childNodes[0];
	if (expandImg.src.endWith('img/actions/add.gif')) {
		alert('Please expand the sub-agreements before auto-populating the counterparty amount.');
		return;
	}
	var umbrellaTrIndex = umbrellaTR.rowIndex;
	var rows = umbrellaTR.parentNode.rows;
	var umbrellaWorkflowId = umbrellaTR.cells[0].childNodes[0].value;
	var umbrellaCcy = umbrellaTR.cells[33].children[0].value;
	var marketDataId = umbrellaTR.cells[62].children[0].value;
	var fundAmts = new Array();
	for ( var i = umbrellaTrIndex; i < rows.length; i++) {
		var row = rows[i];
		var rowId = row.id;
		if (!rowId.startWith(umbrellaWorkflowId + '_fund_')) {
			break;
		}
		var fundWorkflowId = row.cells[0].childNodes[0].value;
		var sysCalcAmt = row.cells[27].children[1].value;
		var fundCcy = row.cells[33].children[0].value;
		var actionId = row.cells[2].childNodes[0].value;
		// convert sys calculate amount to negative when movement is Delivery/Return
		if(actionId == 53 || actionId == 54) {
			//system calculate amount always positive
			sysCalcAmt = "-" + sysCalcAmt;
		}
		var fundAmt = {amtValue:sysCalcAmt, currency:fundCcy, workflowId:fundWorkflowId, actionId:actionId};
		fundAmts.push(fundAmt);
		row.cells[23].childNodes[0].value = sysCalcAmt;
	}
	if(umbrellaActionId == 53 || umbrellaActionId == 54) {
		umbrellaTR.cells[23].childNodes[0].value = "-" + umbrellaTR.cells[27].children[1].value;
		umbrellaTR.cells[24].childNodes[0].value = "-" + umbrellaTR.cells[27].children[1].value;
	} else {
		umbrellaTR.cells[23].childNodes[0].value = umbrellaTR.cells[27].children[1].value;
		umbrellaTR.cells[24].childNodes[0].value = umbrellaTR.cells[27].children[1].value;
	}
	
	
	var dataFromBrowser = umbrellaWorkflowId;
	var callbackProxy = function(dataFromServer) {
		getUmbrellaCcyCtpyAmtCallbackFunc(dataFromServer, dataFromBrowser);
	};
	var callMetaData = {
		callback : callbackProxy
	};
	
	ExternalExposureHelper.getUmbrellaCcyCtpyAmts(fundAmts, umbrellaCcy, marketDataId, callMetaData);
}

function getUmbrellaCcyCtpyAmtCallbackFunc(dataFromServer, dataFromBrowser) {
	var fundAmts = dataFromServer;
	var umbrellaWorkflowId = dataFromBrowser;
	for ( var i = 0; i < fundAmts.length; i++) {
		var fundAmt = fundAmts[i];
		var row = document.getElementById(umbrellaWorkflowId + '_fund_' + fundAmt.workflowId);
		row.cells[24].childNodes[0].value = fundAmt.amtValue;
	}
}

function groupRelativeImplyCtpyAmt(img) {
	var umbrellaTR = img.parentNode.parentNode;
	var expandImg = umbrellaTR.cells[5].childNodes[0];
	if (expandImg.src.endWith('img/actions/add.gif')) {
		alert('Please expand the sub-agreements before auto-populating the counterparty amount.');
		return;
	}
	var umbrellaActionId = umbrellaTR.cells[21].children[0].value;
	if( umbrellaActionId == "120" || umbrellaActionId == "121"){
	    alert('Can not auto populate counterparty amount as requirement amount is 0.');
	    return;
	}
	var umbrellaTrIndex = umbrellaTR.rowIndex;
	var rows = umbrellaTR.parentNode.rows;
	var umbrellaWorkflowId = umbrellaTR.cells[0].childNodes[0].value;
	var agreementId = umbrellaTR.cells[1].childNodes[0].value;
	var umbrellaCcy = umbrellaTR.cells[33].children[0].value;
	var marketDataId = umbrellaTR.cells[62].children[0].value;
	var inputedAmt = umbrellaTR.cells[23].childNodes[0].value;
	var umbrellaSysCalcAmt = umbrellaTR.cells[27].children[1].value;
	if(umbrellaActionId == 53 || umbrellaActionId == 54) {
		umbrellaSysCalcAmt = "-" + umbrellaSysCalcAmt;
	}
	var fundAmts = new Array();
	for (var i = umbrellaTrIndex; i < rows.length; i++) {
		var row = rows[i];
		var rowId = row.id;
		if (!rowId.startWith(umbrellaWorkflowId + '_fund_')) {
			break;
		}
		var fundWorkflowId = row.cells[0].childNodes[0].value;
		var sysCalcAmt = row.cells[27].children[1].value;
		var actionId = row.cells[2].childNodes[0].value;
		// convert sys calculate amount to negative when movement is Delivery/Return
		if(actionId == 53 || actionId == 54) {
			//system calculate amount always positive
			sysCalcAmt = "-" + sysCalcAmt;
		}
		var fundCcy = row.cells[33].children[0].value;
		var fundAmt = {amtValue:sysCalcAmt, currency:fundCcy, workflowId:fundWorkflowId, actionId:actionId};
		fundAmts.push(fundAmt);
	}
	
	var dataFromBrowser = {umbrellaWorkflowId:umbrellaWorkflowId, umbrellaTR:umbrellaTR};
	var callbackProxy = function(dataFromServer) {
		groupRelativeImplyCtpyAmtCallbackFunc(dataFromServer, dataFromBrowser);
	};
	var callMetaData = {
		callback : callbackProxy
	};
	
	ExternalExposureHelper.getRelativeCtpyAmts(agreementId, inputedAmt, umbrellaSysCalcAmt, fundAmts, umbrellaCcy, marketDataId, callMetaData);
	
}

function groupRelativeImplyCtpyAmtCallbackFunc(dataFromServer, dataFromBrowser) {
	var fundAmts = dataFromServer.fundValues;
	var umbrellaWorkflowId = dataFromBrowser.umbrellaWorkflowId;
	for (var i = 0; i < fundAmts.length; i++) {
		var fundAmt = fundAmts[i];
		var row = document.getElementById(umbrellaWorkflowId + '_fund_' + fundAmt.workflowId);
		row.cells[23].childNodes[0].value = fundAmt.amtValue;
		row.cells[24].childNodes[0].value = fundAmt.amtValue2;
	}
	dataFromBrowser.umbrellaTR.cells[23].childNodes[0].value = dataFromServer.totalAmt;
	dataFromBrowser.umbrellaTR.cells[24].childNodes[0].value = dataFromServer.totalAmt;
}

function setInnerText2(node, value) {
	if (node != null && node != undefined && value != null && value != undefined) {
		if(node.childNodes.length >0) {
			setInnerText(node, value);
		} else {
			node.innerText = value;
		}
	}
}

function setInnerText(node, value) {
	if (node != null && node != undefined && value != null && value != undefined) {
		for ( var i = 0; i < node.childNodes.length; i++) {
			var child = node.childNodes[i];
			if (child.nodeType == 3) {
				child.data = value;
			}
		}
	}
}

function setInnerInput(node, value) {
	if (node != null && node != undefined) {
		for ( var i = 0; i < node.childNodes.length; i++) {
			var child = node.childNodes[i];
			if (child.tagName == 'INPUT') {
				child.value = value;
			}
		}
	}
}

dwr.engine.setTextHtmlHandler(function() {
	window.alert("Your session has expired, please login again.");
	document.location = "/firmament/index.do";
});